package org.scboy.dbms.controller;

import org.scboy.common.annotation.Menu;
import org.scboy.common.annotation.Permission;
import org.scboy.common.request.Result;
import org.scboy.dbms.service.BaseServersService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/base/servers")
@Menu("微服务管理系统.服务器管理")
public class BaseServersController {
	
	@Autowired
	BaseServersService service;
	
	@PostMapping("page")
	@Permission("分页查询")
	public Result page(@RequestBody Model params) {
		Page page=service.page(params);
		return Result.Success(page);
	}
	
	@PostMapping("add")
	@Permission("新增")
	public Result add(@RequestBody Model params) {
		boolean result=service.add(params);
		if(result) {
			return Result.Success("SERVER_SERVERS_ADD_SUCCESS");
		}else{
			return Result.Error("SERVER_SERVERS_ADD_ERROR");
		}
	}
	
	@PutMapping("update")
	@Permission("修改")
	public Result update(@RequestBody Model params) {
		boolean result=service.update(params);
		if(result) {
			return Result.Success("SERVER_SERVERS_UPDATE_SUCCESS");
		}else {
			return Result.Error("SERVER_SERVERS_UPDATE_ERROR");
		}
	}
	
	@DeleteMapping("delete")
	@Permission("删除")
	public Result delete(String ip) {
		boolean result=service.delete(ip);
		if(result) {
			return Result.Success("SERVER_SERVERS_DELETE_SUCCESS");
		}else {
			return Result.Error("SERVER_SERVERS_DELETE_ERROR");
		}
	}
	
	@GetMapping("get")
	@Permission("获取详细信息")
	public Result get(String ip) {
		Model data=service.get(ip);
		return Result.Success(data);
	}
	
	@GetMapping("list")
	@Permission("获取所有正常的服务器")
	public Result list() {
		return Result.Success(service.list());
	}
	

	@GetMapping("listAll")
	@Permission("获取所有服务器")
	public Result listAll() {
		return Result.Success(service.listAll());
	}
	

	@GetMapping("health")
	@Permission("获取服务器健康状态")
	public Result health(String ip) {
		return Result.Success(service.health(ip));
	}
	
	@PostMapping("restart")
	@Permission("获取服务器健康状态")
	public Result restart(@RequestBody Model params) {
		service.restart(params.getString("ip"));
		return Result.Success("SERVER_RESTART_SUCCESS");
	}
}
